package com.maxjay.system.web.controller.struts;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.maxjay.common.util.json.JsonParser;

public class AjaxJsonResponse {
	public static final String JSON_ROOT_NODE = "gpms-response";

	public static final String JSON_RESULT_NODE = "result";

	public static final String JSON_REASON_NODE = "reason";

	public static final String JSON_REASON_LOGON_TIMEOUT = "unlogon";

	public static final String JSON_REASON_ACCESS_DENY = "access-deny";

	public static final String JSON_REASON_UNIQUE_CONTRAINT = "unique-contraint";

	public static final String JSON_REASON_BUSINESS_CONTRAINT = "business-contraint";

	public static final String JSON_REASON_SYSTEM_EXCEPTION = "system-exception";

	public static final String JSON_MSG_NODE = "msg";

	public static final String JSON_CUSTOM_CONTENT_NODE = "custom-content";

	public static final String SUCCESSFULLY_STRING = generateSuccessfulOperationDoc();

	public static final String LOGON_TIMEOUT_STRING = generateLogonTimeoutDoc();

	public static final String ACCESS_DENY_STRING = generateAccessDenyDoc();

	public static final String UNIQUE_CONTRAINT_STRING = generateUniqueContraintDoc();

	public static final String BUSINESS_CONTRAINT_STRING = generateBusinessContraintDoc();

	public static final String SYSTEM_EXCEPTION_STRING = generateSystemExceptionDoc();
	
	public AjaxJsonResponse() {
	}

	public static void outputJson(HttpServletResponse response, String json) throws Exception {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		out.flush();
		out.close();
	}

	public static String generateSuccessfulOperationDoc() {
		return generateJsonDocument(generateResultJson(true));
	}

	public static String generateSuccessfulOperationDoc(Map<String, Object> customContent) {
		JSONObject so = generateResultJson(true);
		so.put("custom-content", customContent);
		return generateJsonDocument(so);
	}

	public static String generateSuccessfulOperationDoc(Map<String, Object> customContent, String msg) {
		JSONObject so = generateResultJson(true);
		if (customContent != null && customContent.size() > 0)
			so.put("custom-content", customContent);
		so.put("msg", msg);
		return generateJsonDocument(so);
	}

	public static String generateLogonTimeoutDoc() {
		JSONObject lto = generateResultJson(false);
		lto.put("reason", "unlogon");
		return generateJsonDocument(lto);
	}

	public static String generateAccessDenyDoc() {
		JSONObject ad = generateResultJson(false);
		ad.put("reason", "access-deny");
		return generateJsonDocument(ad);
	}

	public static String generateUniqueContraintDoc() {
		JSONObject uc = generateResultJson(false);
		uc.put("reason", "unique-contraint");
		return generateJsonDocument(uc);
	}

	public static String generateUniqueContraintDoc(String msg) {
		JSONObject uc = generateResultJson(false);
		uc.put("reason", "unique-contraint");
		uc.put("msg", msg);
		return generateJsonDocument(uc);
	}

	public static String generateUniqueContraintDoc(Map<String, Object> customContent) {
		JSONObject uc = generateResultJson(false);
		uc.put("reason", "unique-contraint");
		uc.put("custom-content", customContent);
		return generateJsonDocument(uc);
	}

	public static String generateUniqueContraintDoc(String msg, Map<String, Object> customContent) {
		JSONObject uc = generateResultJson(false);
		uc.put("reason", "unique-contraint");
		uc.put("msg", msg);
		uc.put("custom-content", customContent);
		return generateJsonDocument(uc);
	}

	public static String generateBusinessContraintDoc() {
		JSONObject bc = generateResultJson(false);
		bc.put("reason", "business-contraint");
		return generateJsonDocument(bc);
	}

	public static String generateBusinessContraintDoc(String msg) {
		JSONObject bc = generateResultJson(false);
		bc.put("reason", "business-contraint");
		bc.put("msg", msg);
		return generateJsonDocument(bc);
	}

	public static String generateBusinessContraintDoc(Map<String, Object> customContent) {
		JSONObject bc = generateResultJson(false);
		bc.put("reason", "business-contraint");
		bc.put("custom-content", customContent);
		return generateJsonDocument(bc);
	}

	public static String generateBusinessContraintDoc(String msg, Map<String, Object> customContent) {
		JSONObject bc = generateResultJson(false);
		bc.put("reason", "business-contraint");
		bc.put("msg", msg);
		bc.put("custom-content", customContent);
		return generateJsonDocument(bc);
	}

	public static String generateSystemExceptionDoc() {
		JSONObject se = generateResultJson(false);
		se.put("reason", "system-exception");
		return generateJsonDocument(se);
	}

	private static String generateJsonDocument(JSONObject json) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("json-response", json);
		return JsonParser.getJsonObject(map).toString();
	}

	private static JSONObject generateResultJson(boolean result) {
		JSONObject json = new JSONObject();
		json.put("result", Boolean.valueOf(result));
		return json;
	}

	public static void main(String args[]) {
		System.out.println(SUCCESSFULLY_STRING);
		System.out.println(LOGON_TIMEOUT_STRING);
		System.out.println(ACCESS_DENY_STRING);
		System.out.println(UNIQUE_CONTRAINT_STRING);
		System.out.println(BUSINESS_CONTRAINT_STRING);
		System.out.println(SYSTEM_EXCEPTION_STRING);
		Map<String, Object> successInfo = new HashMap<String, Object>();
		successInfo.put("id", "chenmk");
		successInfo.put("node", "node");
		Map<String, Object> successInfo2 = new HashMap<String, Object>();
		successInfo2.put("id", "chmk35");
		successInfo2.put("node", "hha");
		Map<String, Object> info = new HashMap<String, Object>();
		System.out.println(generateSuccessfulOperationDoc(info));
		info.put("1", successInfo);
		info.put("2", successInfo2);
		JSONArray array = new JSONArray();
		array.add(successInfo);
		array.add(successInfo2);
		System.out.println(array.toString());
		System.out.println(generateSuccessfulOperationDoc(info));
		System.out.println(generateSuccessfulOperationDoc(successInfo));
		System.out.println(generateUniqueContraintDoc("\u7528\u6237\u540D\u79F0\u4E0D\u80FD\u91CD\u590D"));
		System.out.println(generateUniqueContraintDoc("\u7528\u6237\u540D\u79F0\u4E0D\u80FD\u91CD\u590D",
				successInfo));
		System.out.println(generateUniqueContraintDoc(successInfo));
		System.out.println(generateBusinessContraintDoc("\u8FDD\u53CD\u4E1A\u52A1\u903B\u8F91"));
		System.out.println(generateBusinessContraintDoc("\u8FDD\u53CD\u4E1A\u52A1\u903B\u8F91", successInfo));
		System.out.println(generateBusinessContraintDoc(successInfo));
		Map<String, Object> successInfo3 = new HashMap<String, Object>();
		List<Map<String, Object>> successInfoList = new ArrayList<Map<String, Object>>();
		successInfo3.put("sucessList", successInfoList);
		Map<String, Object> successMap = new HashMap<String, Object>();
		successMap.put("id", "CHMK35");
		successInfoList.add(successMap);
		System.out.println(generateSuccessfulOperationDoc(successInfo3));
	}

}
